package classes.view
{
	import classes.OX_Application;
	import classes.controller.DAOSendFlash;
	import classes.controller.EDataObject;
	import classes.view.component.Popup;
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class Mediator_Popup extends Mediator implements IMediator
	{
		public static const NAME:String = "Mediator_Popup";
		public function Mediator_Popup(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			init();
		}
		
		override public function listNotificationInterests():Array
		{
			return [OX_Application.UTIL_CLICK_EVENT,
				OX_Application.CLICK_EVENT,
				OX_Application.READY_SCREEN,
				OX_Application.GAME_OVER,
				OX_Application.INCORRECTQUESTION_EXECUTE,
				OX_Application.THE_FIRST_EXECUTE,
				OX_Application.WAITING_PERSONAL_INFO,
				OX_Application.SINGLE_GAME_PAUSE,
				OX_Application.SINGLE_GAME_OVER,
				OX_Application.SINGLE_POPUP_INCORRECT,
				OX_Application.SINGLE_POPUP_SETTING];
		}
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName()){
				case OX_Application.INCORRECTQUESTION_EXECUTE :
					popup.util_popup_select("alertSingle_1", '');
					break;
				
				case OX_Application.THE_FIRST_EXECUTE :
					popup.util_popup_select("setting", '');
					break;
				
				case OX_Application.UTIL_CLICK_EVENT :
					popup.util_popup_select(notification.getBody() as String, '');
					break;
				
				case OX_Application.CLICK_EVENT :
					if(String(notification.getBody()) == "createroom"){ 
						popup.util_popup_select(notification.getBody() as String, '');
					}else if(String(notification.getBody()) == "subject"){
						popup.util_popup_select(notification.getBody() as String, '');
					}else if(String(notification.getBody()) == "single"){
						popup.util_popup_select(notification.getBody() as String, '');
					}
					break;
				
				case OX_Application.READY_SCREEN :
					if(notification.getBody() == true){
						popup.util_popup_select("readyScreenOn", '');
					}else{
						popup.util_popup_select("readyScreenOff", '');
					}
					break;
				
				case OX_Application.GAME_OVER :
					popup.util_popup_select("gameEnd", notification.getBody());
					break;
				
				case OX_Application.WAITING_PERSONAL_INFO :
					
					popup.util_popup_select("gamePersonal", notification.getBody() as Object);
					break;
				
				case OX_Application.SINGLE_GAME_PAUSE :
					if(notification.getBody() == "pause"){
						popup.util_popup_select("singlePause", notification.getBody() as Object);
					}
					break;
				
				case OX_Application.SINGLE_GAME_OVER :
					popup.util_popup_select("singlegameEnd", notification.getBody() as Object);
					break;
				case OX_Application.SINGLE_POPUP_INCORRECT :
					popup.util_popup_select("alertSingle_2", '');
					break;
				case OX_Application.SINGLE_POPUP_SETTING :
					popup.util_popup_select("single", '');
					break;
			}
		}
		
		
		private function init():void
		{
			popup.addEventListener('ReadyOff',	send_offReadyScreen);
			popup.addEventListener('ScoreOff',  send_offReadyScreen);
			
			popup.addEventListener(EDataObject.EVENT_DATAOBJECT,	eventDataObjectHandler);
			
		}
		private function send_offReadyScreen(e:Event):void
		{
			if(e.type =='ReadyOff' ) sendNotification(OX_Application.READY_SCREEN, false);
			else if(e.type =='ScoreOff') sendNotification(OX_Application.CLICK_EVENT, "scoreOff");
			
		}
		private function eventDataObjectHandler(e:EDataObject):void
		{
			switch(e._object[0]){
				case "createroom" :
					sendNotification(OX_Application.LIST_ROOM_OFF);
					sendNotification(OX_Application.WAITING_ROOM_CREATE);
					break;
				case "singleCreate" :
					sendNotification(OX_Application.LIST_ROOM_OFF);
					sendNotification(OX_Application.SINGLE_ROOM_START);
					break;
				
				case "list" :
					sendNotification(OX_Application.GAME_ROOM_OFF);
					sendNotification(OX_Application.LIST_ROOM_ON);
					break;
				
				case "waiting" :
					sendNotification(OX_Application.GAME_ROOM_OFF);
					sendNotification(OX_Application.WAITING_ROOM_IN);
					break;
				case "singleResume" :
					sendNotification(OX_Application.SINGLE_GAME_PAUSE, "resume");
					break;
				case "singleList":
					sendNotification(OX_Application.SINGLE_ROOM_OFF);
					sendNotification(OX_Application.LIST_ROOM_ON);
					break;
				case "singleScore":
					sendNotification(OX_Application.SINGLE_RE_ON);
					break;
				case "withGame":
					sendNotification(OX_Application.LIST_ROOM_OFF);
					sendNotification(OX_Application.WAITING_ROOM_IN);
					break;
				
				case "startCorrectSingleMode" :
					//오답노트로 실행했을경우.
					sendNotification(OX_Application.SINGLE_ROOM_START);
					break;
				
			}
		}
		
		protected function get popup():Popup
		{
			return viewComponent as Popup;
		}
	}
}